package com.learn.project2.service.admin.admin;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.project2.bean.MarketAdmin;
import com.learn.project2.bean.MarketAdminExample;
import com.learn.project2.mapper.MarketAdminMapper;
import com.learn.project2.mapper.MarketLogMapper;
import com.learn.project2.model.bo.admin.admin.AdminCreateBO;
import com.learn.project2.model.bo.admin.admin.AdminUpdateBO;
import com.learn.project2.model.vo.admin.admin.AdminCreateVO;
import com.learn.project2.model.vo.admin.admin.AdminListAdminVO;
import com.learn.project2.model.vo.admin.admin.AdminListVO;
import com.learn.project2.model.vo.admin.admin.AdminUpdateVO;
import com.learn.project2.utils.AESUtil;
import com.learn.project2.utils.BaseParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author zhizhuke
 * @date 2022-01-08 09-09
 * @version 1.0
 */
@Service
//@CacheConfig(cacheNames = {"marketAdmin"})
public class AdminServiceImpl implements AdminService{

    @Autowired
    MarketAdminMapper adminMapper;
    @Autowired
    MarketLogMapper logMapper;


    /** 
     * @description:
     * @param:
     * @return:  
     * @author zhizhuke
     * @date: 2022-01-08 09:26
     */
//    @Cacheable
    @Override
    public AdminListVO list(BaseParam param,String username) {
        Integer page = param.getPage();
        Integer limit = param.getLimit();
        PageHelper.startPage(page,limit);

        MarketAdminExample example = new MarketAdminExample();
        example.setOrderByClause((param.getSort() + " ") + param.getOrder() + "");
        MarketAdminExample.Criteria criteria = example.createCriteria();
        if (username!=null){
            criteria.andUsernameLike("%"+username+"%");
        }
        criteria.andDeletedEqualTo(false);

        List<MarketAdmin> marketAdmins = adminMapper.selectByExample(example);
        PageInfo<MarketAdmin> pageInfo = new PageInfo<>(marketAdmins);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();

        List<AdminListAdminVO> list = new ArrayList<>();

        for (MarketAdmin marketAdmin : marketAdmins) {
            AdminListAdminVO adminListAdminVO = new AdminListAdminVO(marketAdmin.getId(),marketAdmin.getUsername(),
                    marketAdmin.getAvatar(),marketAdmin.getRoleIds());
            list.add(adminListAdminVO);
        }

        return new AdminListVO(((int) total),pages,limit,page,list);
    }

    /** 
     * @description:
     * @param:
     * @return:  
     * @author zhizhuke
     * @date: 2022-01-08 10:33
     */ 
    @Override
    public AdminCreateVO create(AdminCreateBO adminCreateBO) {
        MarketAdmin marketAdmin = new MarketAdmin();
        marketAdmin.setUsername(adminCreateBO.getUsername());
        marketAdmin.setPassword(adminCreateBO.getPassword());
        marketAdmin.setAvatar(adminCreateBO.getAvatar());
        marketAdmin.setRoleIds(adminCreateBO.getRoleIds());
        Date time = new Date();
        marketAdmin.setAddTime(time);
        marketAdmin.setUpdateTime(time);
        adminMapper.insertSelective(marketAdmin);
        return new AdminCreateVO(marketAdmin.getId(),marketAdmin.getUsername(),marketAdmin.getPassword(),marketAdmin.getAvatar(),
                marketAdmin.getAddTime(),marketAdmin.getUpdateTime(),marketAdmin.getRoleIds());
    }

    /**
     * @description:
     * @param:
     * @return:
     * @author zhizhuke
     * @date: 2022-01-08 10:36
     */
    @Override
    public AdminUpdateVO update(AdminUpdateBO adminUpdateBO) {
        MarketAdmin marketAdmin = new MarketAdmin();
        marketAdmin.setId(adminUpdateBO.getId());
        marketAdmin.setUsername(adminUpdateBO.getUsername());
        marketAdmin.setAvatar(adminUpdateBO.getAvatar());
        marketAdmin.setRoleIds(adminUpdateBO.getRoleIds());
        marketAdmin.setPassword(AESUtil.encrypt(adminUpdateBO.getPassword(),"niupia"));
        Date time = new Date();
        marketAdmin.setUpdateTime(time);

        adminMapper.updateByPrimaryKeySelective(marketAdmin);

        return new AdminUpdateVO(marketAdmin.getId(),marketAdmin.getUsername(),marketAdmin.getAvatar(),
                marketAdmin.getUpdateTime(),marketAdmin.getRoleIds());
    }

    @Transactional
    @Override
    public void delete(AdminUpdateBO adminUpdateBO) {

        MarketAdmin marketAdmin = new MarketAdmin();
        marketAdmin.setId(adminUpdateBO.getId());
        marketAdmin.setDeleted(true);
        adminMapper.updateByPrimaryKeySelective(marketAdmin);

        //增加删除管理员的日志
    }
}
